package com.sparrow.resolver;

import org.thymeleaf.Arguments;
import org.thymeleaf.TemplateProcessingParameters;
import org.thymeleaf.context.IProcessingContext;
import org.thymeleaf.dom.Document;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.util.StringUtils;
import org.thymeleaf.util.Validate;

import java.util.Hashtable;

/**
 * 自定义模板解析器
 * springboot原有的模板解析器只能对应一个文件夹路径, 这里配置多个视图解析器路径
 */
public class CustomTemplateResolver extends SpringResourceTemplateResolver {

    private final Hashtable<String, String> prefixMap = new Hashtable<String, String>();

    private String defaultPrefix = "";

    @Override
    public void setPrefix(String prefix){
        int typeIndex = prefix.indexOf(":");
        if(typeIndex > 0){
            defaultPrefix = prefix.substring(typeIndex + 1);
        }
        super.setPrefix(prefix);
    }

    public void addTemplatePathMap(String key, String path){
        if(path != null){
            if(key.endsWith("/")){
                key = key.substring(0, key.length() - 1);
            }
            if(path.endsWith("/")){
                path = path.substring(0, path.length() - 1);
            }
            prefixMap.put(key, path);
        }
    }

    @Override
    protected String computeResourceName(TemplateProcessingParameters templateProcessingParameters) {
        super.checkInitialized();

        final String templateName = templateProcessingParameters.getTemplateName();
        Validate.notNull(templateName, "Template name cannot be null");
        String unaliasedName = this.getTemplateAliases().get(templateName);
        if (unaliasedName == null) {
            unaliasedName = templateName;
        }

        String prefix = super.getPrefix();
        // ---- 判断模板解析路径 ----
        if(!unaliasedName.startsWith(defaultPrefix)){
            if(unaliasedName.startsWith("/")){
                unaliasedName = unaliasedName.substring(1);
            }

            int splitPos = unaliasedName.indexOf('/');
            String firstPath = unaliasedName.substring(0, splitPos > 0 ? splitPos : unaliasedName.length() - 1);
            String tempPrefix = prefixMap.get(firstPath);
            if(tempPrefix != null){
                prefix = tempPrefix + "/";
                unaliasedName = unaliasedName.substring(splitPos);
            }else{
                IProcessingContext processingContext = templateProcessingParameters.getProcessingContext();
                if(processingContext instanceof Arguments){
                    Arguments arguments = (Arguments) processingContext;
                    Document document = arguments.getDocument();
                    if(document != null){
                        String documentName = document.getDocumentName();
                        splitPos = documentName.indexOf('/');
                        firstPath = documentName.substring(0, splitPos > 0 ? splitPos : documentName.length() - 1);
                        tempPrefix = prefixMap.get(firstPath);
                        if(tempPrefix != null){
                            prefix = tempPrefix + "/";
                        }
                    }
                }
            }
        }
        // ---- 判断模板解析路径 end ----

        final StringBuilder resourceName = new StringBuilder();
        if (!StringUtils.isEmptyOrWhitespace(prefix)) {
            resourceName.append(prefix);
        }

        resourceName.append(unaliasedName);
        if (!StringUtils.isEmptyOrWhitespace(super.getSuffix())) {
            resourceName.append(super.getSuffix());
        }
        return resourceName.toString();
    }

}
